﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;

namespace XCI.Windows.Helper
{
    public static class DXImageHelper
    {
        private static readonly Dictionary<string, Image> Images = new Dictionary<string, Image>();

        public static void Init()
        {
            Images.Clear();

            var assembly = DevExpress.Utils.DxImageAssemblyUtil.ImageAssembly;
            var resources = assembly.GetManifestResourceNames();
            var imageResources = Array.FindAll(resources, resourceName => resourceName.EndsWith(".resources"));

            using (System.Resources.ResourceReader reader = new System.Resources.ResourceReader(assembly.GetManifestResourceStream(imageResources[0])))
            {
                System.Collections.IDictionaryEnumerator dict = reader.GetEnumerator();
                while (dict.MoveNext())
                {
                    string key = (string)dict.Key as string;
                    if (!DevExpress.Utils.DxImageAssemblyUtil.ImageProvider.IsBrowsable(key)) continue;
                    if (key.EndsWith(".png", StringComparison.Ordinal))
                    {
                        var image = Image.FromStream((System.IO.Stream)dict.Value);
                        Images[GetKey(key)] = image;
                    }
                }
            }
        }

        public static Image GetImage(string key)
        {
            Images.TryGetValue(GetKey(key), out Image img);
            return img;
        }

        public static Dictionary<string, Image> GetImages()
        {
            return Images;
        }

        private static string GetKey(string key)
        {
            return key.Replace("%20", " ");//.Replace(" ", string.Empty).Replace("/", string.Empty).Replace("_", string.Empty)
                      //.Replace("x", string.Empty).Replace(".png", string.Empty);
        }
    }
}